package com.example.reservationbilliards.task;

import com.example.reservationbilliards.config.UserConfigLoader;
import com.example.reservationbilliards.pojo.UserConfig;
import com.example.reservationbilliards.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component
public class ScheduledTasks {

    @Autowired
    private ReservationService reservationService;

    @Autowired
    private UserConfigLoader userConfigLoader;

    @Scheduled(cron = "${reservation.cron.expression}") // 从配置文件中读取cron表达式
    //@Scheduled(initialDelay = 1000*10, fixedDelay = 1000 * 60 * 60 * 24)
    public void scheduleReservation() {
        List<UserConfig> userConfigs = userConfigLoader.userConfigs();
        for (UserConfig userConfig : userConfigs) {
            CompletableFuture<Void> future = reservationService.reserveAsync(userConfig);
//            future.join(); // 等待所有任务完成
        }
        System.out.println(123456);
    }
}